﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtPageBuilder;
using ExtSite.Classes;
using Ext.Net;
namespace ExtSite
{
    public partial class Card : BasePage
    {
        ExtCardPage _page = new ExtCardPage();
        protected void Page_Init(object sender, EventArgs e)
        {

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            this._page.LanguageID = "1033";
            string obid = Request.QueryString["ob"];

            string editable = Request.QueryString["edi"];
            if (editable == "1")
            {
                this.tbarEdit.Disable();
                this._page.Editable = true;
            }
            if (string.IsNullOrEmpty(obid))
                return;
            var db = new DataConfigClassesDataContext();
            Classes.Object ob = db.Objects.SingleOrDefault(o => o.ID == int.Parse(obid));
            if (ob != null)
            {
                
                ExtPageBuilder.Object oe = new ExtPageBuilder.Object();
                List<ExtPageBuilder.Object> lObLine = new List<ExtPageBuilder.Object>();
                ClsFunctions.CopyObject(oe, ob);
                oe.Items = new List<ExtPageBuilder.Object_property>();
                List<Classes.Object_property> ops = db.Object_properties.Where(o => o.Object_id == int.Parse(obid)).ToList<Classes.Object_property>();
                
                foreach (Classes.Object_property p in ops)
                {
                    
                    ExtPageBuilder.Object_property o = new ExtPageBuilder.Object_property();
                    ClsFunctions.CopyObject(o, p);
                    oe.Items.Add(o);
                }
                if (!string.IsNullOrEmpty(ob.Link_Condition))
                {
                    JsonObject jsonOLine = JSON.Deserialize<JsonObject>(ob.Link_Condition);
                    foreach (string k in jsonOLine.Keys)
                    {
                        try
                        {
                            Classes.Object oL = db.Objects.SingleOrDefault(q => q.ID == int.Parse(k));
                            if (oL != null)
                            {
                                ExtPageBuilder.Object oeL = new ExtPageBuilder.Object();
                                ClsFunctions.CopyObject(oeL, oL);
                                List<Classes.Object_property> opls = db.Object_properties.Where(o => o.Object_id == int.Parse(k)).ToList<Classes.Object_property>();
                                oeL.Items = new List<ExtPageBuilder.Object_property>();
                                foreach (Classes.Object_property p in opls)
                                {

                                    ExtPageBuilder.Object_property o = new ExtPageBuilder.Object_property();
                                    ClsFunctions.CopyObject(o, p);
                                    oeL.Items.Add(o);
                                }
                                lObLine.Add(oeL);
                            }
                        }
                        catch
                        { }
                    }
                }
                this._page.PageObject = oe;
                this._page.ObjectLines = lObLine;
                this.form1.Controls.Add(this._page.PageStore);
                this.PanelMain.Items.AddRange(this._page.PagePanels.ToArray());
                this.PanelMain.Items.InsertRange(1, this._page.GridLines.ToArray());
                this.ResourceManagerMain.RegisterOnReadyScript(string.Format("setStoreCard({0});", this._page.PageStore.ClientID));
            }
        }
    }
}